<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;

use App\Models\ActivityDiamondCost;
use App\Models\Area;
use App\Models\EMail;

use Encore\Admin\Grid;
use Encore\Admin\Form;
use App\Admin\Extra\Search\Html;
use \Illuminate\Support\Facades\Input;

class ActivityTempController extends Controller
{
    public function anyGetDiamondRebateList() {

        return Admin::content(function (Content $content) {
            $req_param = Input::all();
            $content->header('花费钻石返利');
            $content->description('花费钻石返利');

            $content->row(Html::append([
                Html::dateInput('reward_date', '返利日期', null, Input::get('reward_date', date('Y-m-d', strtotime('-1 day'))) ),
                Html::button('GO', url()->current(), 'search-filter'),
                Html::sjs(\App\Service\Common::build_url_query(url()->current(),  ['area' => $area]))
            ]));

            $content->row(Html::append([

                Html::button('Generate', \App\Service\Common::build_url_query('/admin/acitivity-temp/diamond-rebate-generate',  $req_param), 'confirm-ajax btn-warning'),
                Html::button('Send Reward email', \App\Service\Common::build_url_query('/admin/acitivity-temp/diamond-rebate-reward', $req_param), 'confirm-ajax btn-danger'),

            ]));
            $area_data = \App\Models\Area::areaSelectData(0);
            $grid = Admin::grid(ActivityDiamondCost::class, function(Grid $grid) use($req_param, $area_data) {
                $grid->actions(function($actions){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    // $actions->append('<a href="'.url('admin/areaorder/list-all?cp_area='). $actions->row->cp_area .'"><i class="glyphicon glyphicon-eye-open"></i></a>');
                });
                
                $grid->created_at('日期')->display(function($value) {
                    return $value;
                    // return date('Y-m-d', $value);
                });
                $grid->area('Area Id')->display(function($value)use($area_data) {
                    return $area_data[$value];
                })->sortable();
                $grid->player_id('Player Id')->display(function($value)use($area_data) {
                    return $value;
                });
                $grid->level('level')->display(function($value)use($area_data) {
                    return $value;
                });
                $grid->diamond_cost('花费总钻石')->display(function($value) {
                    return $value;
                });
                $grid->diamond_reward('返利钻石')->display(function($value) {
                    return $value;
                });
                $grid->send_ts('发送次数')->display(function($value) {
                    return intval($value);
                });
                // $grid->model()->orderBy('cp_area', 'DESC');

            });
            $grid->filter(function ($filter) use($area_data){

                $filter->like('player_id', 'player_id')->placeholder('');
                $filter->equal('area', 'Area')->select($area_data);
                $filter->between('created_at', 'Reward Date')->date();

            });
            $content->row($grid->render());
        });
    }

    // 钻石返利数据生产
    public function anyDiamondRebateGenerate() {
        ActivityDiamondCost::generateReward();
        echo json_encode(['code' => 0,'status'=>true, 'msg' => admin_toastr('activity save!','success'), 'data' =>[] ]);
        return;
    }

    public function anyDiamondRebateReward() {
        ActivityDiamondCost::sendToGame();
        echo json_encode(['code' => 0,'status'=>true, 'msg' => admin_toastr('activity save!','success'), 'data' =>[] ]);
        return;
    }
}